package com.jeff.work;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;

public class CalendarNotebook {
    private static CalendarNotebook instance;
    private JFrame frame;
    private JPanel calendarPanel;
    private Map<LocalDate, String> notes;

    private CalendarNotebook() {
        notes = new HashMap<>();
        initialize();
    }

    public static CalendarNotebook getInstance() {
        if (instance == null) {
            instance = new CalendarNotebook();
        }
        return instance;
    }

    private void initialize() {
        frame = new JFrame("Calendar Notebook");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 600);
        frame.setLayout(new BorderLayout());

        calendarPanel = new JPanel(new GridLayout(0, 7));  // 7 columns for days of the week
        frame.add(calendarPanel, BorderLayout.CENTER);

        JPanel navigationPanel = new JPanel();
        JButton prevButton = new JButton("<<");
        JButton nextButton = new JButton(">>");
        navigationPanel.add(prevButton);
        navigationPanel.add(nextButton);
        frame.add(navigationPanel, BorderLayout.NORTH);

        prevButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // Logic for navigating to the previous month
            }
        });

        nextButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // Logic for navigating to the next month
            }
        });

        displayCalendar(LocalDate.now());

        frame.setVisible(true);
    }

    private void displayCalendar(LocalDate date) {
        calendarPanel.removeAll();
        LocalDate firstDayOfMonth = date.withDayOfMonth(1);
        int dayOfWeek = firstDayOfMonth.getDayOfWeek().getValue();

        for (int i = 1; i < dayOfWeek; i++) {
            calendarPanel.add(new JLabel(""));
        }

        int daysInMonth = date.lengthOfMonth();
        for (int day = 1; day <= daysInMonth; day++) {
            JButton dayButton = new JButton(String.valueOf(day));
            LocalDate currentDate = firstDayOfMonth.withDayOfMonth(day);
            dayButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    displayNoteDialog(currentDate);
                }
            });
            calendarPanel.add(dayButton);
        }

        calendarPanel.revalidate();
        calendarPanel.repaint();
    }

    private void displayNoteDialog(LocalDate date) {
        String note = notes.getOrDefault(date, "");
        JTextArea textArea = new JTextArea(note, 10, 30);
        int result = JOptionPane.showConfirmDialog(frame, new JScrollPane(textArea),
                "Note for " + date.toString(), JOptionPane.OK_CANCEL_OPTION);
        if (result == JOptionPane.OK_OPTION) {
            notes.put(date, textArea.getText());
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                CalendarNotebook.getInstance();
            }
        });
    }
}
